home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / VideoToolbox 94.11.17 / Demos / Filter.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-04  |  5.8 KB  |  197 lines  |  [TEXT/KAHL]

  1. /* Filter.c
  2. © 1989-1993 Denis G. Pelli 
  3. Display one letter and low-pass filter it.
  4. Also display a gray wedge, to confirm that lookup table has been correctly loaded.
  5.  
  6. HISTORY:
  7. 3/31/89 dgp Wrote it, based on simple program by Preeti Verghese & Mike Schechter,
  8.             which just displayed a character.
  9. 4/25/89 dgp added code to linearize the clut. Locked the window's pixmap.
  10. 10/9/89 dgp    Updated it to use SetLuminances, etc.
  11. 7/10/90 dgp Changed the declaration of window to be a CWindowPtr.
  12.             Added “done” message.
  13. 10/11/90 dgp Added fpu test.
  14. 10/12/90 dgp Changed LuminanceRecord.h to LuminanceRecord1.h
  15. 2/27/91    dgp     Tidied up.
  16. 8/24/91    dgp    Made compatible with THINK C 5.0.
  17. 2/15/92    dgp    Incorporated Larry Cormack's suggestion of shrinking the console
  18.             so as not to obscure the main window.
  19. 3/10/92    dgp    include mc68881.h
  20. 7/20/92    dgp    added printf of done message
  21. 8/27/92    dgp    replace SysEnvirons() by Gestalt()
  22. 1/4/93    dgp    Don't call SetLuminances if device is of fixedType.
  23. 2/7/93    dgp    Updated to use SetPixelsQuickly. Tidied up the grayscale wedge.
  24.             Recompiled with the new ConvolveX.c, which has better rounding.
  25. 2/8/93    dgp    Recompiled with the new ConvolveX.c, overflow bug now fixed.
  26. 7/7/93    dgp    Made rect a tad bigger to allow for slant of italic.
  27. 6/23/94    dgp    Use YesOrNo().
  28. */
  29. #include "VideoToolbox.h"
  30. #include "Luminance.h"
  31. #include <math.h>
  32. #include <Fonts.h>
  33. #include <assert.h>
  34. #if THINK_C
  35.     #include <console.h>
  36. #endif
  37.  
  38. #define SIZE 127        /* point size of text to be filtered */
  39. #define DIM 64            /* size of point spread function, sigma=DIM/4 */
  40.  
  41. void Filter(void);
  42.  
  43. void main(void)
  44. {
  45.     StackGrow(1000+sizeof(luminanceRecord)+512*sizeof(long));
  46.     Require(gestalt8BitQD);
  47.     Filter();
  48. }
  49. void Filter(void)
  50. {
  51.     EventRecord event;
  52.     register short i;
  53.     int j,clutSize,dim=DIM,error;
  54.     short textSize=SIZE,FontNum;
  55.     unsigned char *s;
  56.     char string[64];
  57.     Rect r,dstRect,wedge;
  58.     CWindowPtr window,oldPort;
  59.     GDHandle device,oldDevice;
  60.     double f[DIM],contrast,a,c,sigma;
  61.     luminanceRecord LR,*LP;
  62.     unsigned long row[512];
  63.     int attenuator;
  64.     RGBColor blackRGB={0,0,0},whiteRGB={0xffff,0xffff,0xffff};
  65.  
  66.     assert(StackSpace()>5000);
  67.     /* Gaussian point spread function, normalized to unit integral. */
  68.     sigma=dim/4.0;
  69.     c=0.0;
  70.     for(i=0;i<dim;i++) {
  71.         a=(i-(dim-1)/2.0)/sigma;
  72.         f[i]=exp(-a*a);
  73.         c+=f[i];
  74.     }
  75.     for(i=0;i<dim;i++) f[i] /= c;
  76.  
  77.     /* INITIALIZE QuickDraw */
  78.     #if THINK_C
  79.         console_options.nrows = 5;
  80.         console_options.left=32;
  81.     #elif __MWERKS__
  82.         MaximizeConsoleHeight();
  83.     #elif
  84.         InitGraf(&qd.thePort);
  85.         InitFonts();
  86.         InitWindows();
  87.         InitCursor();
  88.     #endif
  89.     printf("\n");
  90.     GetGWorld(&oldPort,&oldDevice);
  91.  
  92.     /* Find device corresponding to the experimental screen. */
  93.     for(i=4;i>=0;i--){
  94.         device = GetScreenDevice(i);
  95.         if(device!=NULL) break;
  96.     }
  97.     window = GDOpenWindow(device);
  98.  
  99.     #if __MWERKS__
  100.         SelectWindow((WindowPtr)oldPort);
  101.     #endif
  102.     printf("Welcome to Filter.\n");
  103.     clutSize=GDClutSize(device);
  104.     printf("%d colors.\n",clutSize);
  105.  
  106.     /* Use results of last screen calibration to do gamma correction */
  107.     #include "LuminanceRecord1.h"
  108.     LP=&LR;
  109.     attenuator=Choose(0,"Have you installed an ISR Video Attenuator on that monitor?\n"
  110.         ,noYes,2);
  111.     if(!attenuator){
  112.         LP->r=0.0;
  113.         LP->g=1.0;
  114.         LP->b=0.0;
  115.     }else{
  116.         // if not already in color mode, switch to color mode
  117.         if(!TestDeviceAttribute(device,gdDevType)){
  118.             error=SetDepth(device,(**(**device).gdPMap).pixelSize,1<<gdDevType,1);
  119.         }
  120.     }
  121.     if((*device)->gdType!=fixedType) SetLuminances(device,&LR,0,clutSize-1,0.0,LP->LMax);
  122.  
  123.     contrast = 1.0;
  124.     if(0){
  125.         /* This is how to ask for numbers from the experimenter. */
  126.         /* Supply default value in parentheses. */
  127.         printf("Contrast? (%f) ",contrast);
  128.         gets(string);
  129.         sscanf((char *)string,"%lf",&contrast);
  130.         printf("=%f\n",contrast);
  131.     }
  132.  
  133.     /* Display text */
  134.     SetPort((WindowPtr)window);
  135.     BringToFront((WindowPtr)window);
  136.     PmForeColor(0);                    /* black in our current clut */
  137.     PmBackColor(clutSize-1);        /* white in our current clut */
  138.     EraseRect(&window->portRect);    /* Fill with background color */
  139.     GetFNum((StringPtr)"\pHelvetica",&FontNum);
  140.     TextFont(FontNum);
  141.     TextFace(bold+italic);
  142.     TextSize(textSize);
  143.     j=(unsigned int)((1.0 - contrast)*(clutSize-1));
  144.     PmForeColor(j);
  145.     s = (unsigned char *)"\pHi";
  146.     SetRect(&r,0,0,StringWidth(s),textSize);
  147.     r.right+=0.3*textSize;    // Allow for slant of italic.
  148.     CenterRectInRect(&r,&window->portRect);
  149.     MoveTo(r.left,r.bottom);
  150.     DrawString(s);
  151.  
  152.     #if 1
  153.         /* Write a gray wedge, just for debugging, so we can examine clut */
  154.         SetRect(&wedge,0,0,20,256);
  155.         CenterRectInRect(&wedge,&window->portRect);
  156.         OffsetRect(&wedge,2-wedge.left,0);
  157.         for (j=0;j<256;j++){
  158.             row[0]=(long)clutSize*j/256;
  159.             for(i=0;i<20;i++)row[i]=row[0];
  160.             SetPixelsQuickly(wedge.left,j+wedge.top,row,20);
  161.         }
  162.         InsetRect(&wedge,-1,-1);
  163.         FrameRect(&wedge);
  164.     #endif
  165.  
  166.     /* Filter horizontally */
  167.     SetGDevice(device);                /* Use color table of that screen */
  168.     dstRect=r;                        /* dstRect just contains the string ... */
  169.     InsetRect(&dstRect,-dim/2,0);    /* extend dstRect to allow for the blur */
  170.     HLockHi((Handle)window->portPixMap);
  171.     ConvolveX(f,dim,(BitMap *) *window->portPixMap,(BitMap *) *window->portPixMap,
  172.         &window->portRect,&dstRect);
  173.  
  174.     /* Filter vertically */
  175.     InsetRect(&dstRect,0,-dim/2);    /* Extend dstRect to allow for the blur */
  176.     ConvolveY(f,dim,(BitMap *) *window->portPixMap,(BitMap *) *window->portPixMap,
  177.         &window->portRect,&dstRect);
  178.     SetGDevice(oldDevice);            /* Restore original device */
  179.  
  180.     GetFNum((StringPtr)"\pChicago",&FontNum);
  181.     TextFont(FontNum);
  182.     TextSize(12);
  183.     TextFace(0);
  184.     PmForeColor(0);                /* black in our current clut */
  185.     MoveTo(10,window->portRect.bottom-10);
  186.     DrawString((StringPtr)"\pDone. Click mouse or hit any key to exit.");
  187.     printf("Done. Click mouse or hit any key to exit.\n");
  188.     FlushEvents(everyEvent,0);
  189.     while(!GetNextEvent(keyDownMask+mDownMask,&event)) ;
  190.     SetGWorld(oldPort,oldDevice);
  191.     GDDisposeWindow(window);
  192.     // set back to gray mode
  193.     if(attenuator)error=SetDepth(device,(**(**device).gdPMap).pixelSize,1<<gdDevType,0);
  194.     abort();
  195. }
  196.  
  197.